package cn.source.system.service.impl;

import cn.source.common.constant.HttpStatus;
import cn.source.common.core.domain.entity.SysDept;
import cn.source.common.core.domain.entity.SysRole;
import cn.source.common.core.domain.entity.SysUser;
import cn.source.common.utils.DateUtils;
import cn.source.common.utils.SecurityUtils;
import cn.source.common.utils.StringUtils;
import cn.source.system.domain.*;
import cn.source.system.mapper.*;
import cn.source.system.service.ISysCompanyService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 企业管理Service业务层处理
 *
 * @author sourcebyte.vip
 * @date 2023-10-19
 */
@Service
public class SysCompanyServiceImpl implements ISysCompanyService {
    @Autowired
    private SysCompanyMapper sysCompanyMapper;

    @Autowired
    private SysCompanyMenuMapper sysCompanyMenuMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private SysRoleMenuMapper roleMenuMapper;

    @Autowired
    private SysOperLogMapper operLogMapper;

    @Autowired
    private SysLogininforMapper logininforMapper;

    /**
     * 查询企业管理
     *
     * @param id 企业管理主键
     * @return 企业管理
     */
    @Override
    public SysCompany selectSysCompanyById(Long id) {
        return sysCompanyMapper.selectSysCompanyById(id);
    }

    /**
     * 查询企业管理列表
     *
     * @param sysCompany 企业管理
     * @return 企业管理
     */
    @Override
    public List<SysCompany> selectSysCompanyList(SysCompany sysCompany) {
        List<SysCompany> list = sysCompanyMapper.selectSysCompanyList(sysCompany);
        for (SysCompany company : list) {
            SysOperLog operLog = new SysOperLog();
            operLog.setOperName(company.getUserName());
            operLog = operLogMapper.selectLastOperLog(operLog);
            if (StringUtils.isNotNull(operLog)) {
                company.setLastOptDate(operLog.getOperTime());
            }
            SysLogininfor logininfor = new SysLogininfor();
            logininfor.setUserName(company.getUserName());
            logininfor = logininforMapper.selectLastLogininfor(logininfor);
            if (StringUtils.isNotNull(logininfor)) {
                company.setLastLoginTime(logininfor.getLoginTime());
            }
        }
        return list;
    }

    /**
     * 新增企业管理
     *
     * @param sysCompany 企业管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysCompany(SysCompany sysCompany) {
        sysCompany.setCreateTime(DateUtils.getNowDate());
        int result = sysCompanyMapper.insertSysCompany(sysCompany);
        // 创建企业时，创建部门(使用企业名称)
        SysDept sysDept = new SysDept();
        sysDept.setDeptName(sysCompany.getName());
        sysDept.setLeader(sysCompany.getLinkMan());
        sysDept.setPhone(sysCompany.getLinkPhone());
        sysDept.setCompanyId(sysCompany.getId());
        sysDept.setCreateTime(DateUtils.getNowDate());
        deptMapper.insertDept(sysDept);
        // 创建角色(企业管理员)
        SysRole sysRole = new SysRole();
        sysRole.setRoleName("企业管理员");
        sysRole.setRoleKey("company");
        sysRole.setRoleSort("1");
        sysRole.setStatus("0");
        sysRole.setCompanyId(sysCompany.getId());
        sysRole.setCreateTime(DateUtils.getNowDate());
        roleMapper.insertRole(sysRole);
        // 创建用户(设置企业id)
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(sysDept.getDeptId());
        sysUser.setUserName(sysCompany.getUserName());
        sysUser.setNickName(sysCompany.getName());
        sysUser.setPhonenumber(sysCompany.getLinkPhone());
        sysUser.setPassword(SecurityUtils.encryptPassword(sysCompany.getPassword()));
        sysUser.setCompanyId(sysCompany.getId());
        sysUser.setCreateTime(DateUtils.getNowDate());
        userMapper.insertUser(sysUser);
        List<SysUserRole> userRoleList = Lists.newArrayList();
        SysUserRole userRole = new SysUserRole();
        userRole.setUserId(sysUser.getUserId());
        userRole.setRoleId(sysRole.getRoleId());
        userRoleList.add(userRole);
        userRoleMapper.batchUserRole(userRoleList);
        return result;
    }

    /**
     * 修改企业管理
     *
     * @param sysCompany 企业管理
     * @return 结果
     */
    @Override
    public int updateSysCompany(SysCompany sysCompany) {
        sysCompany.setUpdateBy(SecurityUtils.getUsername());
        sysCompany.setUpdateTime(DateUtils.getNowDate());
        return sysCompanyMapper.updateSysCompany(sysCompany);
    }

    /**
     * 批量删除企业管理
     *
     * @param ids 需要删除的企业管理主键
     * @return 结果
     */
    @Override
    public int deleteSysCompanyByIds(Long[] ids) {
        return sysCompanyMapper.deleteSysCompanyByIds(ids);
    }

    /**
     * 保存企业菜单
     */
    @Override
    @Transactional
    public int insertCompanyMenu(Long companyId, Long[] menuIds) {
        SysCompany company = sysCompanyMapper.selectSysCompanyById(companyId);
        SysUser user = userMapper.selectUserByUserName(company.getUserName());
        Long roleId = user.getRoles().get(0).getRoleId();
        List<SysCompanyMenu> menuList = Lists.newArrayList();
        List<SysRoleMenu> roleMenus = Lists.newArrayList();
        for (Long menuId : menuIds) {
            SysCompanyMenu companyMenu = new SysCompanyMenu();
            companyMenu.setCompanyId(companyId);
            companyMenu.setMenuId(menuId);
            menuList.add(companyMenu);
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(roleId);
            roleMenus.add(roleMenu);
        }
        sysCompanyMenuMapper.deleteCompanyMenuByCompanyId(companyId);
        roleMenuMapper.deleteRoleMenuByRoleId(roleId);
        if (menuList.size() > 0) {
            sysCompanyMenuMapper.batchCompanyMenu(menuList);
            // 给企业管理员设置菜单
            roleMenuMapper.batchRoleMenu(roleMenus);
        }
        return HttpStatus.SUCCESS;
    }

    @Override
    public List<Long> selectMenuListByCompanyId(Long companyId) {
        return sysCompanyMenuMapper.selectMenuListByCompanyId(companyId);
    }

    /**
     * 企业授权
     */
    @Override
    public int agentSysCompany(SysCompany sysCompany) {
        if (StringUtils.isNull(sysCompany.getEffectiveStartDate())) {
            sysCompany.setEffectiveStartDate(DateUtils.getNowDate());
        }
        if (StringUtils.isNull(sysCompany.getEffectiveEndDate())) {
            sysCompany.setEffectiveEndDate(DateUtils.getNowDate());
        }
        sysCompany.setUpdateBy(SecurityUtils.getUsername());
        sysCompany.setUpdateTime(DateUtils.getNowDate());
        return sysCompanyMapper.updateSysCompany(sysCompany);
    }
}
